﻿using System;
using System.Globalization;

namespace RayTracer
{
    public class Color
    {
        private const double scale = 1.0 / 255;

        public double R;
        public double G;
        public double B;

        public Color(double r, double g, double b)
        {
            this.R = r;
            this.G = g;
            this.B = b;
        }

        public byte[] ToByteArray()
        {
            return new byte[4]
            {
                (byte)(0xff * Math.Min(this.B, 1.0)),
                (byte)(0xff * Math.Min(this.G, 1.0)),
                (byte)(0xff * Math.Min(this.R, 1.0)),
                0xff            
            };
        }
        public System.Windows.Media.Color ToWMColor()
        {
            return new System.Windows.Media.Color()
            {
                R = (byte)(0xff * Math.Min(this.R, 1.0)),
                G = (byte)(0xff * Math.Min(this.G, 1.0)),
                B = (byte)(0xff * Math.Min(this.B, 1.0)),
                A = 0xff
            };
        }
        public static Color FromWMColor(System.Windows.Media.Color color)
        {
            return new Color(scale * color.R, scale * color.G, scale * color.B);
        }

        public static Color operator +(Color color1, Color color2)
        {
            return Add(color1, color2);
        }
        public static Color operator *(Color color, double scalar)
        {
            return Multiply(color, scalar);
        }
        public static Color operator *(double scalar, Color color)
        {
            return Multiply(color, scalar);
        }
        public static Color operator *(Color color1, Color color2)
        {
            return Multiply(color1, color2);
        }

        public static Color Add(Color color1, Color color2)
        {
            return new Color(color1.R + color2.R, color1.G + color2.G, color1.B + color2.B);
        }
        public static Color Multiply(Color color, double scalar)
        {
            return new Color(color.R * scalar, color.G * scalar, color.B * scalar);
        }
        public static Color Multiply(Color color1, Color color2)
        {
            return new Color(color1.R * color2.R, color1.G * color2.G, color1.B * color2.B);
        }

        public override string ToString()
        {
            return string.Format(NumberFormatInfo.InvariantInfo, "({0:0.000},{1:0.000},{2:0.000})", R, G, B);
        }

        public static readonly Color Black = new Color(0, 0, 0);
        public static readonly Color White = new Color(1, 1, 1);
        public static readonly Color Red = new Color(1, 0, 0);
        public static readonly Color Green = new Color(0, 1, 0);
        public static readonly Color Blue = new Color(0, 0, 1);
        public static readonly Color Yellow = new Color(1, 1, 0);
        public static readonly Color Magenta = new Color(1, 0, 1);
        public static readonly Color Cyan = new Color(0, 1, 1);

    }
}
